package com.d2.pay.utils;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class Helper {
	
	/**
	 * 获取订单信息
	 * @param mode 支付模式
	 * @param type 支付类型
	 * @param amount 支付金额
	 * @return 订单信息
	 */
	public static Map<String, Object> getOrderInfo(String mode, int type, String amount) {
		// 此处填写商户的 uid
	    // 必填
	    String payMerchId = "0360240996769265";

	    // 此处填写商户的 token
	    // 必填
	    String payMerchToken = "poar79dk4w4y6xa7byamfdkd1d00koj0";

	    // 支付金额
	    // 必填
	    String payAmount = amount;

	    // 收款账号
	    // 选填
	    String payAccount = "";

	    // 支付类型，支付宝 = 1，微信 = 2
	    // 必填
	    int payType = type;

	    // 支付模式，标准模式 = 1，自定义模式 = 2
	    // 必填
	    String payMode = mode;

	    // 支付成功后异步通知地址，必须是公网地址
	    // 必填
	    String payNotifyUrl = "http://yourdomain/notify.do";

	    // 支付成功后同步跳转地址，必须是公网地址
	    // 该参数仅标准模式需要指定。
	    String payReturnUrl = "";
	    if("1".equals(payMode)){
	        payReturnUrl = "http://yourdomain/return.do";
	    }

	    // 自定义订单号
	    // 选填
	    String payOrderNum = getRandomCode(12);

	    // 自定义用户编号
	    // 选填
	    String payOrderUid = getRandomCode(12);

	    // 商品名称
	    // 选填
	    String payGoodName = "test good name";

	    // 将上述参数进行 md5-32 加密
	    String md5Str = payGoodName + payType + payAccount + payNotifyUrl + payReturnUrl + payOrderNum + payOrderUid + payAmount + payMode + payMerchToken + payMerchId;
	    String payKey = md5(md5Str);
	    
	    Map<String, Object> payData = new HashMap<>();
	    
	    payData.put("uid", payMerchId);
	    payData.put("amount", payAmount);
	    payData.put("account", payAccount);
	    payData.put("type", payType);
	    payData.put("mode", payMode);
	    payData.put("notifyurl", payNotifyUrl);
	    payData.put("returnurl", payReturnUrl);
	    payData.put("ordernum", payOrderNum);
	    payData.put("orderuid", payOrderUid);
	    payData.put("goodname", payGoodName);
	    payData.put("key", payKey);
	    
	    return payData;
	}
	
	/**
	 * 获取指定长度随机字符串
	 * @param len 字符串长度
	 * @return 随机字符串
	 */
	public static String getRandomCode(int len) {
		String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < len; i++) {
			int number = random.nextInt(62);
			sb.append(str.charAt(number));
		}
		return sb.toString();
	}
	
	/**
	 * md5 
	 * @param plainText 原始字符串
	 * @return md5 结果
	 */
	public static String md5(String plainText) {
        byte[] secretBytes = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            secretBytes = md.digest();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("no md5 module find.");
        }
        String md5code = new BigInteger(1, secretBytes).toString(16);
        for (int i = 0; i < 32 - md5code.length(); i++) {
            md5code = "0" + md5code;
        }
        return md5code;
    }
}
